import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Solution {
    public static int[] smallestK(int[] arr, int k) {
        System.out.println(arr.length);
        int[] a =new int[k];
        if(k==0){
            return a;
        }
        PriorityQueue<Integer> queue = new PriorityQueue(new Comparator<Integer>(){
            public int compare(Integer o1,Integer o2){
                return o2-o1;
            }
        });
        for(int i = 0;i<k;i++){
            queue.offer(arr[i]);
        }
        for(int i = k;i<arr.length;++i){
            System.out.println(k);
            if (queue.peek() > arr[i]) {
                queue.poll();
                queue.offer(arr[i]);
            }
            System.out.println(i);
        }
        for(int i = 0;i<k;i++){
            a[i] = queue.poll();
        }
        return a;
    }

    public static void main(String[] args) {
        int[] arr = {1,3,5,7,2,4,6,8};
        System.out.println(Arrays.toString(smallestK(arr,4)));
    }
}
